package com.whz.workmanageol.service.impl;

import com.whz.workmanageol.dao.*;
import com.whz.workmanageol.entity.*;
import com.whz.workmanageol.service.PublishService;
import com.whz.workmanageol.service.WorkService;
import com.whz.workmanageol.utils.UUIDGenerator;
import com.whz.workmanageol.vo.FilePath;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import java.util.*;

@Service
@Transactional
public class PublishServiceImpl implements PublishService {
    @Autowired
    private PublishMapper publishMapper;
    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private WorkService workService;

    /**
     * 将publish插入数据库 返回publishid
     * @param filePath
     * @param publish
     * @return
     */
    private String savePublish(FilePath filePath, Publish publish) {
        publish.setId(UUIDGenerator.getUUID());
        publish.setSeq(filePath.getSeq());
        publish.setFilepath(filePath.toString());
        publish.setFilename(filePath.getSeq());
        publish.setSuffix(".zip");
        publish.setTeacherid(filePath.getTeacherid());
        publish.setGradeid(filePath.getGradeid());
        publish.setCourseid(filePath.getCourseid());
        publish.setPublishtime(new Date());
        publishMapper.insert(publish);
        return publish.getId();
    }

    /**
     * 发布作业对应的数据库操作
     * @param filePath
     * @param publish
     */
    @Override
    public void publishDB(FilePath filePath, Publish publish) {
        // 1.插入publish
        String publishid = savePublish(filePath, publish);
        // 2.插入work
        // 获取该班级学生学号
        Example example = new Example(Student.class);
        example.createCriteria().andEqualTo("gradeid", filePath.getGradeid());
        List<Student> studentList = studentMapper.selectByExample(example);
        workService.saveWork(filePath, publishid, studentList);
    }

    @Override
    public List<Publish> publishList(String teacherId, String gradeid, String courseid) {
        List<Publish> publishList = new ArrayList<>();
        Example example = new Example(Publish.class);
        example.createCriteria()
                .andEqualTo("teacherid", teacherId)
                .andEqualTo("gradeid", gradeid)
                .andEqualTo("courseid", courseid);
        publishList = publishMapper.selectByExample(example);
        Collections.sort(publishList, Comparator.comparing(Publish::getSeq));
        return publishList;
    }


    @Override
    public Long countTeacherPub(FilePath filePath) {

        Example example = new Example(Publish.class);
        example.createCriteria()
                .andEqualTo("teacherid", filePath.getTeacherid())
                .andEqualTo("gradeid", filePath.getGradeid())
                .andEqualTo("courseid", filePath.getCourseid());
        long count = publishMapper.selectCountByExample(example) + 1;
        return count;
    }
}
